const Service = require('egg').Service;
const jwt = require('jsonwebtoken');

class UsersService extends Service {
  async login() {
    const { app, ctx } = this;
    const { stuid, username, password } = ctx.request.body;
    // 用户输入学号  姓名都可以登陆
    let sql = 'select * from users where 1=1 '
    if (stuid) { // 
      sql += `and stuid='${stuid}' and password='${ctx.helper.getPassword(password)}'`
    } else {
      sql += `and username='${username}' and password='${ctx.helper.getPassword(password)}'`
    }
    let data = await app.mysql.query(sql)
    let token = jwt.sign({ ...data[0] }, app.config.keys, { expiresIn: '24h' })
    return { code: 0, token }
  }
  // 学生注册表


}

module.exports = UsersService;